package ar.edu.unlp.info.tesis.controladores.lrs;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import ar.edu.unlp.info.tesis.controladores.GenericController;
import ar.edu.unlp.info.tesis.database.service.ActorService;
import ar.edu.unlp.info.tesis.database.service.GenericService;
import ar.edu.unlp.info.tesis.domain.Actor;
import ar.edu.unlp.info.tesis.domain.Agent;
import ar.edu.unlp.info.tesis.domain.Group;


@Controller
@RequestMapping(value = "/actor")
public class ActorController extends GenericRestController{

	@RequestMapping(value = "/create", method = RequestMethod.PUT, consumes = {MediaType.APPLICATION_JSON_VALUE}, produces = {MediaType.APPLICATION_JSON_VALUE})
	@ResponseBody
	public Actor create(@RequestBody Actor actor) {
		new ActorService().save(actor);
		return actor;
	}
	
	@RequestMapping(value = "/save", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_JSON_VALUE}, produces = {MediaType.APPLICATION_JSON_VALUE})
	@ResponseBody
	public Actor save(@RequestBody Actor newDataActor) {
		ActorService actorService = new ActorService();
		Actor actor = (Actor)actorService.getById(newDataActor.getId());
		BeanUtils.copyProperties(newDataActor, actor);
		actorService.save(actor);
		return actor;
	}
	
	@RequestMapping(value = "/delete/{idActor}", method = RequestMethod.DELETE)
	@ResponseBody
	public void delete(@PathVariable String idActor) {
		ActorService actorService = new ActorService();
		Actor actor = (Actor)actorService.getById(idActor);
		actorService.remove(actor);
	}
		
	@RequestMapping(value = "/getGroups", method = RequestMethod.GET, headers = "Accept=application/json")
	@ResponseBody
	public List<Group> getGroups() {
		return GenericService.getService(Group.class).getList();
	}
	
	@RequestMapping(value = "/getAgents", method = RequestMethod.GET, headers = "Accept=application/json")
	@ResponseBody
	public List<Agent> get() {
		return GenericService.getService(Agent.class).getList();
	}
	
	@RequestMapping(value = "/getGroups/{idAgent}", method = RequestMethod.GET, headers = "Accept=application/json")
	@ResponseBody
	public List<Group> getGroups(@PathVariable String idAgent) {
		return new ActorService().getGroups(idAgent);
	}
	
	@RequestMapping(value = "/get/{idActor}", method = RequestMethod.GET, headers = "Accept=application/json")
	@ResponseBody
	public Actor get(@PathVariable String idActor) {
		return (Actor)new ActorService().getById(idActor);
	}

	@RequestMapping(value = "/addToGroup/{idGroup}/{idAgent}", method = RequestMethod.PUT)
	@ResponseBody
	public void addAgentToGroup(@PathVariable String idGroup, @PathVariable String idAgent)
	{
		ActorService service = new ActorService();
		service.addAgentToGroup(idGroup, idAgent);
	}
	
	@RequestMapping(value = "/removeFromGroup/{idGroup}/{idAgent}", method = RequestMethod.DELETE)
	@ResponseBody
	public void removeAgentFromGroup(@PathVariable String idGroup, @PathVariable String idAgent)
	{
		ActorService service = new ActorService();
		service.removeAgentFromGroup(idGroup, idAgent);
	}
}
